* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/merged_for_reg, clear

	xtset ctycode year
	
********************************************************************************
* Contemporaneous Value Estimates(Figure 4)
********************************************************************************

* set reference bin

	rename dd5bin10 REF_BIN
	
	tempfile r2 r3
******north*****
	foreach x in maize  wheat rice vegetable soybean   {
		 qui reghdfe ln_`x'_yield dd5bin* ///
					c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==1,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store north_`x'_yld_5C
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")
			
			preserve 
			use `"`r2'"',clear
			gen crop = "`x'"
			rename id  region
			save $temp/est_crop_north_`x',replace
			restore			
			}	
	
	*esttab north_*_yld_5C, b(4) se(4) keep(dd*bin*) brackets
	
****south*****
	foreach x in rice vegetable oil wheat  maize  {
		 qui reghdfe ln_`x'_yield dd5bin*  ///
					c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
			i.pro#c.year##c.year ///
			if inrange(year,1995,2015) & region3==2,  ///
			absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store south_`x'_yld_5C
		parmest , label saving(`"`r3'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")	

			preserve 
			use `"`r3'"',clear
			gen crop = "`x'"
			rename id  region
			save $temp/est_crop_south_`x',replace
			restore			
		}	
	
	*esttab south_*_yld_5C, b(4) se(4) keep(dd*bin*) brackets	

	
	clear
	foreach x in maize  wheat rice vegetable soybean {
		append using $temp/est_crop_north_`x'
	}
	foreach x in rice vegetable oil wheat  maize {
		append using $temp/est_crop_south_`x'
	}	
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = 2.5 if strmatch(parm, "dd5bin0")
	replace level = 7.5 if strmatch(parm, "dd5bin5")
	replace level = -2.5 if strmatch(parm, "dd5binNeg")
	*replace level = 12.5 if strmatch(parm, "dd5bin10")
	replace level = 17.5 if strmatch(parm, "dd5bin15")
	replace level = 22.5 if strmatch(parm, "dd5bin20")
	replace level = 27.5 if strmatch(parm, "dd5bin25")
	replace level = 32.5 if strmatch(parm, "dd5bin30")
	replace level = 37.5 if strmatch(parm, "dd5bin35")

	
	// crop name 
	replace crop = "Corn" if strmatch(crop,"maize")
	replace crop = "Rice" if strmatch(crop,"rice")
	replace crop = "Wheat" if strmatch(crop,"wheat")
	replace crop = "Oil Crop" if strmatch(crop,"oil")
	replace crop = "Vegetable" if strmatch(crop,"vegetable")
	replace crop = "Soybean" if strmatch(crop,"soybean")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort region crop : gen last = _n == _N
	expand 2 if last
	sort region crop parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]		
	
	save $visual/visual_cropping,replace	
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}	
